/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div style="margin-top:10px">
    <div
      id="explanation"
      class="explanation"
      style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;"
    >
      <div class="bckopa-tips">
        <div class="title">
          <img src="@/static/images/handd.png" alt />
          <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        </div>
        <ul>
          <li>文章中的图片请使用插件上传，不要使用复制粘贴的方式。</li>
        </ul>
      </div>
      <span title="收起提示" id="explanationZoom" style="display: block;"></span>
    </div>
    <div style="margin-top:10px">
      <el-form ref="form" :model="form" label-width="200px">
        <el-form-item label="标题" required>
          <el-input v-model="form.title" maxlength="64" size="small"></el-input>
          <p class="err-msg" v-if="errResult.title && !form.title">{{errResult.title}}</p>
          <p class="notic">最多64个字</p>
        </el-form-item>
        <el-form-item label="作者">
          <el-input v-model="form.author" maxlength="8" size="small" show-word-limit></el-input>
          <p class="notic">非必填，最多8个字</p>
        </el-form-item>
        <el-form-item label="正文" required>
          <Ueditor :value="ueditor.value" :config="ueditor.config" ref="ue" v-if="ueditor.isShow"></Ueditor>
          <p
            class="err-msg"
            v-if="errResult.content && $refs.ue.getUEContent() == ''"
          >{{errResult.content}}</p>
          <p class="notic">不多于2万字</p>
        </el-form-item>
        <el-form-item label="摘要">
          <el-input
            type="textarea"
            v-model="form.digest"
            size="small"
            maxlength="120"
            show-word-limit
            :autosize="{ minRows: 2, maxRows: 6}"
            style="width:350px"
          ></el-input>
          <p class="notic">选填，如果不填则抓取正文前64字。不多于120字。</p>
        </el-form-item>
        <el-form-item label="原文链接">
          <el-input v-model="form.content_source_url" size="small"></el-input>
          <p class="notic">链接前请带上 http:// 或者 https:// ，不填则不显示</p>
        </el-form-item>
        <el-form-item label="封面" required>
          <el-upload
            class="avatar-uploader"
            :action="apiHead + '/mall/upload/wechat/media?type=thumb'"
            :show-file-list="false"
            :on-success="handleSuccess"
            :before-upload="beforeUpload"
          >
            <el-image
              v-if="form.thumb_url"
              :src="form.thumb_url"
              style="width: 100px; height: 100px"
            ></el-image>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <p
            class="err-msg"
            v-if="errResult.thumb_media_id && !form.thumb_media_id"
          >{{errResult.thumb_media_id}}</p>
          <p class="notic">请上传图片格式文件</p>
        </el-form-item>
        <el-form-item label="显示封面">
          <el-switch v-model="form.show_cover_pic" :active-value="1" :inactive-value="0"></el-switch>
          <p class="notic">是否在正文开始处显示封面</p>
        </el-form-item>
        <el-form-item label="是否打开评论">
          <el-switch v-model="form.need_open_comment" :active-value="1" :inactive-value="0"></el-switch>
          <p class="notic">当公众号具备留言功能的权限时，此设置才生效</p>
        </el-form-item>
        <el-form-item v-if="form.need_open_comment" label="是否粉丝才可评论">
          <el-switch v-model="form.only_fans_can_comment" :active-value="1" :inactive-value="0"></el-switch>
          <p class="notic">评论范围（所有人，或仅公众号粉丝可以留言）</p>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit()" :loading="isLoading">确认提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {} from "@/utils/api";
import Ueditor from "@/components/ueditor/index";
export default {
  data() {
    return {
      form: {
        material_id: this.$route.params.material_id,
        desc: "",
      },
      ueditor: {
        isShow: false,
        value: "",
        config: {
          serverUrl:
            this.apiHead +
            "/mall/ueditor/config?path=wx_news&is_wechat=true",
          zIndex: 999,
          initialFrameWidth: "80%", //初化宽度
          initialFrameHeight: 300, //初化高度
          focus: false, //初始化时，是否让编辑器获得焦点true或false
          maximumWords: 99999,
          removeFormatAttributes:
            "class,style,lang,width,height,align,hspace,valign", //允许的最大字符数 'fullscreen',
          pasteplain: false, //是否默认为纯文本粘贴。false为不使用纯文本粘贴，true为使用纯文本粘贴
          autoHeightEnabled: true,
        },
      },
      errResult: {},
      isLoading: false,
    };
  },
  created() {
    if (this.$route.params.id) {
      this.getWxNews();
    } else {
      this.ueditor.isShow = true;
    }
  },
  components: {
    Ueditor,
  },
  methods: {
    getWxNews() {
      var that = this;
      this.request
        .get("/mall/wx_news", {
          params: {
            id: this.$route.params.id,
          },
        })
        .then(function (res) {
          that.ueditor.value = res.content;
          that.form = res;
          that.ueditor.isShow = true;
        });
    },
    handleSuccess(res) {
      this.form.thumb_url = res.result;
      this.form.thumb_media_id = res.media_id;
      this.form.wx_url = res.url;
      this.$forceUpdate();
    },
    beforeUpload(file) {
      const isPic =
        file.type === "image/gif" ||
        file.type === "image/jpg" ||
        file.type === "image/jpeg" ||
        file.type === "image/bmp" ||
        file.type === "image/png";
      const isLt10M = file.size / 1024 / 1024 < 10;

      if (!isPic) {
        this.$message.error("上传图片只能是 JPG、JPEG、PNG、BMP、GIF 格式!");
      }
      if (!isLt10M) {
        this.$message.error("上传图片大小不能超过 10MB!");
      }
      return isPic && isLt10M;
    },
    onSubmit() {
      this.isLoading = true;
      this.form.content = this.$refs.ue.getUEContent();
      var that = this;
      if (this.form.id) {
        //编辑
        this.request.put("/mall/wx_news", that.form).then(function (res) {
          if (res.status == 1) {
            that.$message.success({
              message: "保存成功",
              duration: 1000,
              onClose: function () {
                that.$router.push({ name: "wx_news_list" });
              },
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      } else {
        //添加
        this.request.post("/mall/wx_news", that.form).then(function (res) {
          if (res.status == 1) {
            that.$message.success({
              message: "添加成功",
              duration: 1000,
              onClose: function () {
                that.$router.push({ name: "wx_news_list" });
              },
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      }
    },
  },
};
</script>

<style scoped>
.avatar-uploader-icon {
  line-height: 100px;
}
>>> .el-input__inner {
  width: 350px;
}
</style>
